package com.sghy.xcxservice.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.sghy.xcxservice.common.CustomException;
import com.sghy.xcxservice.common.Error;
import com.sghy.xcxservice.common.STS;
import com.sghy.xcxservice.controller.BaseController;
import com.sghy.xcxservice.dto.UserInfoVo;
import com.sghy.xcxservice.entity.BgbOrgUser;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;

import static com.sghy.xcxservice.common.TokenInterceptor.getUid;

@RequestMapping("/user")
@RestController
public class userController extends BaseController {
    
    /**
     * 定位打卡
     * @param latitude
     * @param longitude
     * @param desc
     * @param type  enum('location','photo')
     * @param photos '照片',
     * @return
     */
    @PostMapping("location")
    public Map<String, Object> location(@RequestParam double latitude, @RequestParam double longitude,
                                        @RequestParam String description, @RequestParam String type,
                                        @RequestParam(required = false) String photos) {
        if (!"location".equals(type) && !"photo".equals(type)) {
            throw new CustomException(Error.PARAM_ERROR, "type参数错误");
        }
        
        clockInService.location(getUid(), latitude, longitude, description, type, photos);
        return SUS("定位打卡成功");
    }
    
    @PostMapping("savaUserHeadIco")
    public Map<String, Object> savaUserHeadIco( @RequestParam String headIco) {
        if ( "".equals(headIco)) {
            throw new CustomException(Error.PARAM_ERROR, "更新头像地址错误");
        }
        
        orgUserService.savaUserHeadIco(getUid(),headIco);
        return SUS("更新头像成功");
    }
    
    /**
     * 查询我的今天的打卡记录列表
     */
    @PostMapping("getMyTodayClockIn")
    public Map<String, Object> getMyTodayClockIn() {
        return SUS(clockInService.getMyTodayClockIn(getUid()));
    }
    
    /**
     * 查询我的历史打卡记录，需要分页查询及支持选择那一天查询
     */
    @PostMapping("getMyHistoryClockIn")
    public Map<String, Object> getMyHistoryClockIn(@RequestParam(defaultValue = "1") int page,
                                                   @RequestParam(defaultValue = "20") int size,
                                                   @RequestParam(required = false) String date) {
        if (date != null && !date.matches("\\d{4}-\\d{2}-\\d{2}")) {
            throw new CustomException(Error.PARAM_ERROR, "日期格式错误，正确格式为yyyy-MM-dd");
        }
        return SUS(clockInService.getMyHistoryClockIn(getUid(), page, size, date));
    }
    
    /**
     * 查询用户历史打卡记录，需要分页查询及支持选择那一天查询
     */
    @PostMapping("getUserHistoryClockIn")
    public Map<String, Object> getUserHistoryClockIn(@RequestParam(defaultValue = "1") int page,
                                                   @RequestParam(defaultValue = "20") int size,
                                                     @RequestParam String uId,
                                                   @RequestParam(required = false) String date) {
        if (date != null && !date.matches("\\d{4}-\\d{2}-\\d{2}")) {
            throw new CustomException(Error.PARAM_ERROR, "日期格式错误，正确格式为yyyy-MM-dd");
        }
        BgbOrgUser user = orgUserService.getByUid(getUid());
        if (user.getAdmin() != 1) {
            throw new CustomException(Error.NO_ROLE, "没有权限");
        }
        return SUS(clockInService.getMyHistoryClockIn(uId, page, size, date));
    }
    
    
    /**
     * 查询所有人的打卡记录列表，按照日期查询，支持分页查询，日期必填
     */
    @PostMapping("getAllClockIn")
    public Map<String, Object> getAllClockIn(@RequestParam String date,
                                              @RequestParam(defaultValue = "1") int page,
                                              @RequestParam(defaultValue = "20") int size) {
        if (!date.matches("\\d{4}-\\d{2}-\\d{2}")) {
            throw new CustomException(Error.PARAM_ERROR, "日期格式错误，正确格式为yyyy-MM-dd");
        }
        return SUS(clockInService.getAllClockIn(date, page, size));
    }
    
    /**
     * 查询所有的用户列表，返回List<UserInfoVo>,还需要每个人打卡的总记录数量
     */
    @PostMapping("getAllUserList")
    public Map<String, Object> getAllUserList(@RequestParam(defaultValue = "1") int page,
                                               @RequestParam(defaultValue = "20") int size) {
        return SUS(orgUserService.getAllUserList(page, size));
    }
    
    
    /**
     * 查询所有的管理员
     */
    @PostMapping("getAllAdminList")
    public Map<String, Object> getAllAdminList(@RequestParam(defaultValue = "1") int page,
                                                @RequestParam(defaultValue = "20") int size) {
        return SUS(orgUserService.getAllAdminList(page, size));
    }
    
    /**
     * 通过模糊搜索查询成员
     */
    @PostMapping("searchUser")
    public Map<String, Object> searchUser(@RequestParam String like) {
        if (like == null || like.isEmpty()) {
            throw new CustomException(Error.PARAM_ERROR, "请输入关键字");
        }
        return SUS(orgUserService.searchUser(like));
    }
    
    /**
     * 取消或者设置一个用户的管理员身份
     */
    @PostMapping("setAdmin")
    public Map<String, Object> setAdmin(@RequestParam String uId, @RequestParam boolean isAdmin) {
        if (uId == null || uId.isEmpty()) {
            throw new CustomException(Error.PARAM_ERROR, "用户ID不能为空");
        }
        BgbOrgUser user = orgUserService.getByUid(getUid());
        if (user.getAdmin() != 1) {
            throw new CustomException(Error.NO_ROLE, "没有权限");
        }
        if (isAdmin) {
            orgUserService.setAdmin(uId);
            return SUS("设置管理员成功");
        } else {
            orgUserService.cancelAdmin(uId);
            return SUS("取消管理员成功");
        }
    }
    
    /**
     * 查询今天的打卡用户
     */
    @PostMapping("getTodayClockInUser")
    public Map<String, Object> getTodayClockInUser() {
        return SUS(clockInService.getTodayClockInUser());
    }
    
    /**
     * 获取oss的sts临时密钥
     */
    @RequestMapping(value = "/getStsAuth")
    public Map<String, Object> getStsAuth() {
        JSONObject sts = STS.sts();
        return SUS(sts);
    }

}
